﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
///Agent 的摘要说明
/// </summary>
public class Agent : User
{

    Component mComponent;
    string agentID;
    string schoolID;
    string telephonenumber;
    string acpassword;


    public Component MComponent
    {
        get { return mComponent; }
        set { mComponent = value; }
    }


    public string AgentID
    {
        get { return agentID; }
        set { agentID = value; }
    }
    
  

    public string SchoolID
    {
        get { return schoolID; }
        set { schoolID = value; }
    }

  
    public string Telephonenumber
    {
        get { return telephonenumber; }
        set { telephonenumber = value; }
    }

   

    public string Acpassword
    {
        get { return acpassword; }
        set { acpassword = value; }
    }

    public Agent(Component component)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        this.mComponent = component;
	}

    public Agent(
        Component component,
        string agentID,
        string schoolID,
        string telephonenumber,
        string acpassword)
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        this.mComponent = component;
        this.agentID = agentID;
        this.schoolID = schoolID;
        this.telephonenumber = telephonenumber;
        this.acpassword = acpassword;
    }



    public override string insert(System.Data.SqlClient.SqlConnection sqlcon)
    {
        try
        {
            User user = (User)this.mComponent;

            string[] mydata ={SqlFormat.ap(agentID),SqlFormat.ap(schoolID),
                          SqlFormat.ap(user.Idcard),SqlFormat.ap(user.Name),""+user.Sex,SqlFormat.ap(user.Birthday),
                      SqlFormat.ap(telephonenumber),SqlFormat.ap(acpassword)};
            string sqlCmdStr = SqlFormat.exeProcedureNotap("inputAgent", mydata);

            SqlCommand sqlCmd = new SqlCommand(sqlCmdStr, sqlcon);

            if (sqlCmd.ExecuteNonQuery() == 0)
            {
                return null;
            }

            return "ok";
        }
        catch (System.Exception ex)
        {
            return null;
        }
       
    }

    public string fillDataById(System.Data.SqlClient.SqlConnection sqlcon)
    {
        try
        {
            string sqlcmd = " SELECT * FROM AgentView WHERE agentID=" + SqlFormat.ap(agentID) + ";";
            SqlDataAdapter sqladt = new SqlDataAdapter();
            DataTable dts = new DataTable();
            sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
            sqladt.Fill(dts);

            if (dts.Rows.Count != 1)
            {
                return null;
            }

            agentID = dts.Rows[0].ItemArray[0].ToString();
            schoolID = dts.Rows[0].ItemArray[5].ToString();
            telephonenumber = dts.Rows[0].ItemArray[6].ToString();
            if (mComponent != null)
            {
                User user = (User)mComponent;
                user.Name = dts.Rows[0].ItemArray[1].ToString();
                user.Sex = dts.Rows[0].ItemArray[2].ToString();
                user.Birthday = dts.Rows[0].ItemArray[3].ToString();
                user.Idcard = dts.Rows[0].ItemArray[4].ToString();
            }

            return "ok";
        }
        catch (System.Exception ex)
        {
            return ex.Source;
        }
       
    }

    public static bool isExist(System.Data.SqlClient.SqlConnection sqlcon,String myAgentID)
    {

        string sqlcmd = " SELECT agentID FROM AgentView WHERE agentID=" + SqlFormat.ap(myAgentID) + ";";
        SqlDataAdapter sqladt = new SqlDataAdapter();
        DataTable dts = new DataTable();
        sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
        sqladt.Fill(dts);

        if (dts.Rows.Count != 1)
        {
            return false;
        }

        return true;
    }

    public  bool checkIDCard(System.Data.SqlClient.SqlConnection sqlcon)
    {

        if (base.idcard == null)
            return false;
        string sqlcmd = " SELECT agentID FROM AgentView WHERE IDCard=" + SqlFormat.ap(base.idcard) + ";";
        SqlDataAdapter sqladt = new SqlDataAdapter();
        DataTable dts = new DataTable();
        sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
        sqladt.Fill(dts);

        if (dts.Rows.Count != 1)
        {
            return false;
        }



        return true;
    }

    public static object[] getDataByIDAndPsw(SqlConnection sqlcon, string mAccount, string mPassword)
    {
        try
        {
            string sqlcmd = " SELECT * FROM AgentView WHERE "
           + "agentID=" + SqlFormat.ap(mAccount)
           + "AND acpassword=" + SqlFormat.ap(mPassword) + ";";
            SqlDataAdapter sqladt = new SqlDataAdapter();
            DataTable dts = new DataTable();
            sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
            sqladt.Fill(dts);

            if (dts.Rows.Count != 1)
            {
                return null;
            }
            return dts.Rows[0].ItemArray;
        }
        catch (System.Exception ex)
        {
            return null;
        }
       

        
       
    }

}